﻿$(window).on('resize', onResizeHandle);
$(document).ready(function () {
    onResizeHandle();
});
setTimeout(function () {
    onResizeHandle();
}, 300);
window.staticurl='/?m=shopmobile&c=test&a=';
//$(document).on('touchmove',function (e) {
//    e.preventDefault();e.stopPropagation();
//});
function onResizeHandle() {
    var width = $(window).width();
    if (width > 640) width = 640;
    if (width == null || width == undefined || width == '' || width == 0) {
        setTimeout(onResizeHandle, 300);
        return;
    }
    $('html').css({'fontSize': 12 * width / 320 + 'px'});
    $('.loading').remove();
    $('.app').show();
    width = $('.index .apps .title').width();
    if (width != undefined && width != 0) {
        $('html').css({'fontSize': 12 * width / 320 + 'px'});
    }
    window.size = 16 * width / 320;
}


/*********显示字数***************/

function deleteSelected(text, len, r) {
    if (text == undefined) return '';
    var retext;
    if (getStrLength(text) > len) {
        for (var i = parseInt(len / 2); i < len; i++) {
            var cc = text.substr(0, i);
            if (getStrLength(cc) > len) {
                break;
            }
            retext = cc;
        }
    } else {
        retext = text;
    }

    if (retext != text && r != true) {
        retext += '...';
    }

    return retext
}


function getStrLength(str) {
    var cArr = str.match(/[^\x00-\xff]/ig);
    return str.length + (cArr == null ? 0 : cArr.length);
}

/*******************************************提交判断 ******************************************/

function selectedInput(str) {
    var arr = $(str);
    var errorArr = [];
    var rww;
    for (var i = 0; i < $(str).length; i++) {
        rww = {};
        rww.error = false;
        rww.errortext = $(arr[i]).attr('data-rule-error');
        bixuan($(arr[i]));
        errorArr.push(rww);
    }

    function bixuan(str) {
        if (str.attr('data-rule-required') == 'true') {
            var cc = str.val();
            if (cc == '') {
                rww.required = true;
                rww.error = true;
            }
        }
        if (str.attr('data-rule-Mobile') == 'true') {
            var cc = str.val();
            if (textTel(cc) == false) {
                rww.mobile = true;
                rww.error = true;
            }
        }
        if (str.attr('data-rule-Mail') == 'true') {
            var cc = str.val();
            if (emailCheck(cc) == false) {
                rww.mail = true;
                rww.error = true;
            }
        }
        if (str.attr('data-rule-mima') == 'true') {
            var cc = str.val();
            if (mimaCheck(cc) == false) {
                rww.mima = true;
                rww.error = true;
            }
        }
    }

    return errorArr;
}

/**
 * 获取提交对象
 * @constructor
 */
function getInputData(id, data) {
    var arr = $(id);
    for (var i = 0; i < arr.length; i++) {
        if ($(arr[i]).is('select')) {
            data[$(arr[i]).attr('name')] = $(arr[i]).find(' option:selected').text();
        } else {
            data[$(arr[i]).attr('name')] = $(arr[i]).val();
        }
    }
}

/**
 * 判断手机号格式
 * @param tel
 * @returns {boolean}
 */
function textTel(tel) {
    var reg = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
    if (reg.test(tel)) {
        return true
    } else {
        return false
    }
    ;
}

/**
 * Check email format
 */
function emailCheck(str) {
    var pattern = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
    if (!pattern.test(str)) {
        return false;
    }
    return true;
}

function mimaCheck(str) {
    var reg = /^[0-9a-zA-Z]+$/;
    if (!reg.test(str)) {
        return false;
    } else {
        return true;
    }
}

/**
 * jquery的ajax请求
 * @constructor
 */
function AjaxPost() {
    var _this = this;
    this.sendScore = send;
    function send(url, datas, fun) {
        ajax_post(url, datas, function (data) {
            $('.loading2').hide();
            fun(data);
        });
    }


    function ajax_post(url, data, fun) {
        var _url = url;
        $.ajax({
            type: "POST",
            url: _url,
            data: data,
            dataType: 'json',
            success: function (msg) {
                fun(msg);
            },
            error: function () {
                var data = {"code": {"errcode": "10086", "errmsg": "网络错误"}, "msg": "网络错误"};
                fun(data);
            }
        });
    }
}

function addshareHandle(url,imgurl,title,wtitle,fun){
    document.addEventListener('WeixinJSBridgeReady',onBridgeReady , false);
    try{
        if(WeixinJSBridge!=undefined){
            onBridgeReady();
        }
    }catch(e){

    }
    function onBridgeReady() {
        window.shareData = {
            "timeLineLink": url,
            "sendFriendLink": url,
            "weiboLink": url,
            "tTitle": title,
            "tContent": wtitle,
            "fTitle": title,
            "fContent": wtitle,
            "wContent": wtitle
        };
        // 发送给好友
        WeixinJSBridge.on('menu:share:appmessage', function (argv) {
            WeixinJSBridge.invoke('sendAppMessage', {
                "img_url": imgurl,
                "img_width": "200",
                "img_height": "200",
                "link": window.shareData.sendFriendLink,
                "desc": window.shareData.fContent,
                "title": window.shareData.fTitle
            }, function (res) {
                //alert('ss:'+window.shareData.sendFriendLink+'/'+window.shareData.fContent+'/'+window.shareData.fTitle);
                //alert(res.err_msg);
                validateShare(res,0);
                //_report('send_msg', res.err_msg);
            });
        });
        // 分享到朋友圈
        WeixinJSBridge.on('menu:share:timeline', function (argv) {
            WeixinJSBridge.invoke('shareTimeline', {
                "img_url": imgurl,
                "img_width": "200",
                "img_height": "200",
                "link": window.shareData.timeLineLink,
                "desc": window.shareData.tContent,
                "title": window.shareData.tTitle
            }, function (res) {
                validateShare(res,1);
                //_report('timeline', res.err_msg);
            });
        });

    }

    function validateShare(res,num) {
        if(res.err_msg != 'send_app_msg:cancel' && res.err_msg != 'share_timeline:cancel') {
            //分享完毕回调
            window.diandata.elementid="share";
            window.diandata.eventtype="share";
            if(num==0){
                window.diandata.pagename="bigwheelShare";
            }else{
                window.diandata.pagename="bigwheelShare";
            }
            dadian.inite(window.urldian,window.diandata);
            if(fun!=undefined){
                fun();
            }
        }
    }
}

/****************复制对象*********************/
function copydata(obj) {
    var o = {};
    for (var p in obj) {
        var name = p;//属性名称
        var value = obj[p];//属性对应的值
        o[name] = obj[p];
    }
    return o;
}


/***********************显示弹窗*************************/
function showTC(mc) {
    mc.show();
    middle_mc(mc);
    mc.find('.btn_close').on('click', function () {
        mc.find('.btn_close').off('click');
        mc.hide();
    });

    function middle_mc(mc) {
        var minmc = mc.find('.con');
        var h = mc.height();
        var minh = minmc.height();
        minmc.css({'marginTop': (h - minh) / 2});
    }
}


/**********************打点请求********************/
var dadian = new Dadian();
function Dadian() {
    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return { //移动终端浏览器版本信息
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                wPhone: u.indexOf('Windows Phone') > -1, //是否window phone
            };
        }(),
    };
    this.inite = function (url, d) {
        if (browser.versions.iPhone) {
            d.platform = "iphone";
        } else if (browser.versions.iPad) {
            d.platform = "iPad";
        } else if (browser.versions.android) {
            d.platform = "android";
        } else if (browser.versions.wPhone) {
            d.platform = "windowsPhone";
        } else {
            d.platform = "others";
        }
        d.timestamp = Date.parse(new Date());
        $.get(url, d, function () {

        });
    }
}


/**
 * 打点类型配置
 * @param str
 */
function dianchange(str) {
    window.diandata.elementid = 'click';
    window.diandata.eventtype = 'tap';
    window.diandata.pagename = str;
    dadian.inite(window.urldian, window.diandata);
}

/***
 * 声音格式识别
 * @param str
 * @returns {Audio}
 */
function shibiemp3(str) {
    var _audio = new Audio();
    if (_audio.canPlayType('audio/mp3') != '') {
        _audio.src = str + '.mp3';
    } else {
        _audio.src = str + '.ogg';
    }
    return _audio;
}


/********添加点击事件*****************/
function adPointdian(mc) {
    var _this = this;
    mc.addEventListener('mousedown', onMouseDownhandle);
    mc.addEventListener('mouseout', onendhandle);
    mc.addEventListener('pressup', onendhandle);
    mc.addEventListener('rollout', onendhandle);
    mc.addEventListener('removed', _this.remove);

    function onMouseDownhandle() {
        mc.gotoAndStop(1);
    }

    function onendhandle() {
        mc.gotoAndStop(0);
    }

    this.remove = function () {
        console.log('rwwwx');
        mc.removeEventListener('mousedown', onMouseDownhandle);
        mc.removeEventListener('mouseout', onendhandle);
        mc.removeEventListener('pressup', onendhandle);
        mc.removeEventListener('rollout', onendhandle);
    }
}
